﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Kiss.Silverlight.OrderService;

namespace Kiss.Silverlight
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void getButton_Click(object sender, RoutedEventArgs e)
        {
            OrderServiceClient service = new OrderServiceClient();
            service.GetCustomersByCityCompleted += new EventHandler<GetCustomersByCityCompletedEventArgs>(service_GetCustomersByCityCompleted);
            service.GetCustomersByCityAsync(cityTextBox.Text);
        }

        private void service_GetCustomersByCityCompleted(object sender, GetCustomersByCityCompletedEventArgs e)
        {
            dataGrid.ItemsSource = e.Result;
        }

        private void updateButton_Click(object sender, RoutedEventArgs e)
        {
            OrderServiceClient service = new OrderServiceClient();
            Customer customer = (Customer)dataGrid.SelectedItem;
            if(!customer.City.EndsWith("X"))
                customer.City = customer.City + "X";
            else
                customer.City = customer.City.Remove(customer.City.Length - 1);
            service.UpdateCustomerCompleted += new EventHandler<AsyncCompletedEventArgs>(service_UpdateCustomerCompleted);
            service.UpdateCustomerAsync(customer);
        }

        private void service_UpdateCustomerCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {            
        }
    }
}
